<?php
$pacth="C:/xampp/htdocs/05_NIV/";
require ($pacth.'DataProvider/DataConnection.php');
Class _ContactModule{
	var $_connect;
	function _ContactModule() {
        $this->_connect = new _DataConnection();
    }
	function insertContact($_name,$_email,$_country,$_phone,$_address,$_type,$_content){
        $contentencode=htmlentities($_content,ENT_QUOTES,'utf-8');
		$SQLCommand="
        insert 
			`contact`(
				`name`,`email`,`country`,`phone`,`address`,`type`,`content`,`contentencode`,`insert`,`log`
				)
			values(
				'$_name','$_email','$_country','$_phone','$_address','$_type','$_content','$contentencode',now(),''
				)
        ";
        $n=$this->_connect->executeNoneSQL($SQLCommand);
        if($n==1) {
            return true;
        }
        return false;
    }
	function getContacts($n,$p,$trash){
        $SQLCommand="
			SELECT *,if(`viewer` like '%;".$_SESSION[ "JO_account_key"			]."%',0,1) as `isnew`,
				(select group_concat(`account`.`name` separator ',') AS `group_concat(``account``.``name`` separator ',')` from `account` where `contact`.viewer like concat('%;',`account`.`key`,';%')) as viewers,
				DATEDIFF(now(),`insert`) as dateiff
			FROM `contact`
			".(($trash)?" where `delete` is not null":"where `delete` is null ")."
			ORDER BY `insert` DESC
			LIMIT ".$p*$n.",$n
		";
        $data=$this->_connect->executeSQL($SQLCommand);
        $dt=array();
        if($data!=null) while ($row=$data->fetch_array()){
                array_push($dt, $row);
        }else        return null;
        return $dt;
    }
	function getContacts_numpage($n){
        $SQLCommand="select count(*) as numrow from `contact` where `delete` is null";
        $data=$this->_connect->executeSQL($SQLCommand);
        if($data!=null ) {
            $row=$data->fetch_array();
            return ($row["numrow"]%$n==0)?(int)($row["numrow"]/$n):((int)($row["numrow"]/$n)+1);
        }
        return 0;
    }
	function getContacts_numrows($trash){
        $SQLCommand="select count(*) as numrow from `contact` ".(($trash)?" where `delete` is not null":"where `delete` is null");
        $data=$this->_connect->executeSQL($SQLCommand);
        if($data!=null ) {
            $row=$data->fetch_array();
            return $row["numrow"];
        }
        return 0;
    }
	function viewContact($key,$account){
        $contentencode=htmlentities($_contentco,ENT_QUOTES,'utf-8');
        $acc=";".$account.";";
		$SQLCommand="
        update 
			`contact`
		set `viewer`=concat(if(isnull(`viewer`),'',`viewer`),'$acc')
		where `key`=$key
			and if(isnull(`viewer`),'',`viewer`) not like '%$acc%'
        ";
		
        $n=$this->_connect->executeNoneSQL($SQLCommand);
        if($n==1) {
            return true;
        }
        return false;
    }
	function updatelabel($key,$label,$name,$acc){
        $SQLCommand="
        update 
			`contact`
		set `label`='$label;$name;$acc'
		where `key`=$key
		";
		
        $n=$this->_connect->executeNoneSQL($SQLCommand);
        if($n==1) {
            return true;
        }
        return false;
    }
	function delete($key,$name,$acc){
        $SQLCommand="
        update 
			`contact`
		set `delete`=now(),`log`=concat(`log`,'$name($acc):delete (',now(),')\n')
		where `key`=$key
		";
		
        $n=$this->_connect->executeNoneSQL($SQLCommand);
        if($n==1) {
            return true;
        }
        return false;
    }
	function restore($key,$name,$acc){
        $SQLCommand="
        update 
			`contact`
		set `delete`=null,`log`=concat(`log`,'$name($acc):restore (',now(),')\n')
		where `key`=$key
		";
		
        $n=$this->_connect->executeNoneSQL($SQLCommand);
        if($n==1) {
            return true;
        }
        return false;
    }
	function sendmaillog(){
        $SQLCommand="
        update 
			`contact`
		set `log`=concat(`log`,'Cant send mail.(',now(),')\n')
		order by `insert` desc limit 1
		";
		
        $n=$this->_connect->executeNoneSQL($SQLCommand);
        if($n==1) {
            return true;
        }
        return false;
    }
}
?>